package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.domain.Order;
import com.itheima.reggie.domain.OrderDetail;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.AddressService;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    //地址
    @Autowired
    AddressService addressService;

    //购物车
    @Autowired
    CartService cartService;

    //订单明细
    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    OrderMapper orderMapper;


    @Override
    public void submit(Order order) {
        //获取用户id
        Long userId = UserHolder.get().getId();
        //收获地址对象
        Address address = addressService.findById(order.getAddressId());
        //购物车对象
        List<Cart> cartList = cartService.list();

        //雪花id
        Long orderId = IdWorker.getId();

        BigDecimal amount = new BigDecimal("0");

        //明细
        for (Cart cart : cartList) {
            //创建一个orderDetail装一个购物项的明细(一个购物车中有多个购物项)
            OrderDetail orderDetail = new OrderDetail();
            //设置明购物项名
            orderDetail.setName(cart.getName());
            //设置明细购物项图片
            orderDetail.setImage(cart.getImage());
            //设置明细中的订单(order)id(手动生成雪花id)
            orderDetail.setOrderId(orderId);
            //设置明细菜品id
            orderDetail.setDishId(cart.getDishId());
            //设置明细套餐id
            orderDetail.setSetmealId(cart.getSetmealId());
            //设置明细购物项菜品口味
            orderDetail.setDishFlavor(cart.getDishFlavor());
            //设置明细购物项中的个数
            orderDetail.setNumber(cart.getNumber());
            //设置明细购物项的单价
            orderDetail.setAmount(cart.getAmount());
            //保存订单明细
            orderDetailMapper.insert(orderDetail);

            //计算订单支付金额
            amount = amount.add(orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber())));
        }

        //保存订单
        order.setId(orderId);
        order.setNumber(orderId.toString());
        order.setStatus(1);
        order.setUserId(userId);
        order.setOrderTime(new Date());
        order.setCheckoutTime(new Date());
        order.setAmount(amount);
        order.setPhone(address.getPhone());
        order.setAddress(address.getDetail());
        order.setConsignee(address.getConsignee());
        orderMapper.insert(order);

        cartService.delete();
    }

    @Override
    public Page<Order> selectByLimit(Integer page, Integer pageSize, Long number, Date beginTime, Date endTime) {
        Page<Order> orderPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Order> qw = new LambdaQueryWrapper<>();
        qw.eq(number != null,Order::getId,number);
        qw.between(beginTime != null,Order::getOrderTime,beginTime,endTime);

        orderPage = orderMapper.selectPage(orderPage,qw);


        return orderPage;
    }

    @Override
    public Page<Order> selectAppHistoryByLimit(Integer page, Integer pageSize) {
        Long id = UserHolder.get().getId();
        LambdaQueryWrapper<Order> qw = new LambdaQueryWrapper<>();
        qw.eq(Order::getUserId,id);
        qw.orderByDesc(Order::getOrderTime);
        Page<Order> orderPage = new Page<>(page,pageSize);
        orderPage = orderMapper.selectPage(orderPage,qw);

        for (Order record : orderPage.getRecords()) {
            LambdaQueryWrapper<OrderDetail> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(OrderDetail::getOrderId,record.getId());

            List<OrderDetail> orderDetails = orderDetailMapper.selectList(qw2);

            record.setOrderDetails(orderDetails);
        }

        return orderPage;
    }
}
